//
//  CZComposePictrueView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let CZComposePictrueViewCellId = "CZComposePictrueViewCell"

class CZComposePictrueView: UICollectionView {
    
    //声明一个闭包
    var closure:(()->())?
    
    //设置一个存图片的数组
    var imageList:[UIImage] = [UIImage]()
    
    //提供供外界添加图片的方法
    
    func addImages(image:UIImage){
        //代表有图片添加 显示collectionView
        self.hidden = false
        //添加到数组中
        imageList.append(image)
        //刷新UI
        reloadData()
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - 设置UI
    func setupUI(){
        //刚开始进来的时候让collectionView隐藏
        self.hidden = true
        
        
        backgroundColor = UIColor.whiteColor()
        //设置代理
        dataSource = self
        delegate = self
        //注册
        registerClass(CZComposePictrueViewCell.self, forCellWithReuseIdentifier: CZComposePictrueViewCellId)
        //设置流水布局
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置大小布局
        let cellMargin:CGFloat = 5
        //宽高
        let cellWH:CGFloat = (SCREENW - 20 - 2*cellMargin)/3
        
        //设置Item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置间距
        layout.minimumLineSpacing = cellMargin
        layout.minimumInteritemSpacing = cellMargin
        
    }
    
}

//MARK: - 代理方法
extension CZComposePictrueView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        /*
         - 如果 显示为 0 或者为 9 没有加号图片
         - 反之 其他 后面 显示加号图片
         */
        let count = imageList.count
        if count == 0 || count == 9{
            return count
        }
        return imageList.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView .dequeueReusableCellWithReuseIdentifier(CZComposePictrueViewCellId, forIndexPath: indexPath) as! CZComposePictrueViewCell
        
        if imageList.count == indexPath.item {
            cell.image = nil
        }else{
            //赋值
            cell.image = imageList[indexPath.item]
            
            //删除按钮闭包的实例化
            cell.closure = {[weak self] in
                
                //闭包的执行
                self?.imageList.removeAtIndex(indexPath.item)
                //判断有没有图片
                if self?.imageList.count == 0 {
                    //隐藏控件
                    self?.hidden = true
                }
                
                self?.reloadData()
                
            }
            
        }
        
        
        return cell
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        if imageList.count == indexPath.item {
            closure?()
        }
        
        
        
    }
    
}



//MARK: - 自定义Cell

class CZComposePictrueViewCell: UICollectionViewCell {
    
    //声明一个闭包
    var closure:(()->())?
    
    
    //提供供外界调用的方法
    var image:UIImage?{
        
        didSet{
            
            if image == nil {
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                //没有图片不显示删除按钮
                deleteBtn.hidden = true
            }else{
                
                bgImageView.image = image
                //解决cell复用问题
                bgImageView.highlightedImage = nil
                deleteBtn.hidden = false
            }
            
            
        }
    }
    
    //MARK: - 删除按钮的监听
    @objc private func deletBtnClick(){
        
        print("删除按钮的点击")
        //闭包的调用
        closure?()
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //Mark: - 设置自定义Cell 的UI
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteBtn)
        
        //添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        deleteBtn.snp_makeConstraints { (make) in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    
    //MARK: - 懒加载控件
    
    private lazy var bgImageView:UIImageView = UIImageView()
    
    //懒加载删除按钮
    private lazy var deleteBtn:UIButton = {
       let btn = UIButton()
        
        btn.setImage(UIImage(named:"compose_photo_close"), forState: UIControlState.Normal)
        //添加点击事件
        btn.addTarget(self, action: "deletBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
}










